package com.ebupt.migu.music.iop.controller;

import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.iop.service.IopGroupServce;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @author wz
 */
@Slf4j
@RestController
@RequestMapping("/group")
public class iopGroupController {

    @Resource
    private IopGroupServce  iopGroupServce;

    @Resource
    private ReqestParamUtil reqestParamUtil;

    @Autowired
    private IDAuthUtils idAuthUtils;

    /**
     * 通过标签创建人群
     *
     * @return
     */
    @RequestMapping("/create")
    public ResultObject createByLabel(HttpServletRequest request) {
        Map<String, Object> paramMap = reqestParamUtil.getParams2(request);
        return iopGroupServce.createByLabel(paramMap);
    }


    /**
     * 更新人群信息
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/update")
    public ResultObject updateCrowd(HttpServletRequest request) {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminByToken(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权刷新此人群");
        }
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return iopGroupServce.updateCrowd(params);
    }

    /**
     * 删除人群
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/delete")
    public ResultObject deleteCrowd(HttpServletRequest request) {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminByToken(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权删除此人群");
        }
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return iopGroupServce.deleteCrowd(params);
    }


    /**
     * 删除人群
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/data")
    public ResultObject data(HttpServletRequest request) {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return iopGroupServce.data(params);
    }



}
